using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.AI.Node;

namespace com.yoozoo.gta.Gameplay.Liberty.AI.Node.Npc.Condition
{
    public class CheckTeammateHasTakeDamage: AIBaseNode
    {

        public float DetectionRadius = 10f;
        protected override void OnExecute()
        {
            int count = 0;
            Collider[] results = PhysicsUtils.OverlapSphereNonAlloc(out count, _entity.viewer.viewerRoot.position, DetectionRadius, BattleAgent.BoxingOnHitLayerMaskName);
            if (count > 0)
            {
                foreach (var collider in results)
                {
                    if (collider != null)
                    {
                        var target = BattleAgent.TryGetAIEntityFromCollider(collider);
                        if (target!=null)
                        {
                            if (_entity.data.side == target.data.side) //检查队友有没有敌对目标
                            {
                                var enemy = target.battleAgent.GetTarget(TargetType.CurrentLockTarget);
                                var hasRevengeTarget = target.battleAgent.revengeTarget;
                                if (enemy != null)
                                {
                                    _entity.battleAgent.SetTarget(TargetType.CurrentLockTarget,enemy);
                                    _entity.battleAgent.SetTarget(TargetType.ChaseRevengeTarget,enemy);
                                    EndAction(true);
                                    return;
                                }

                                if (hasRevengeTarget!=null)//如果有复仇目标，就设置为复仇目标
                                {
                                    _entity.battleAgent.SetTarget(TargetType.CurrentLockTarget,hasRevengeTarget);
                                    _entity.battleAgent.SetTarget(TargetType.ChaseRevengeTarget,hasRevengeTarget);
                                    EndAction(true);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            EndAction(false);
        }
    }
}